
let global = getApp().globalData;
let wxApi = global.wxApi;
let util = global.util;
Page({
  data: {
    openExplain: false,
    levels: [{
      id: 1,
      level: 1,
      second: 60,
      active: false
    },{
      id: 2,
      level: 2,
      second: 50,
      active: false
    },{
      id: 3,
      level: 3,
      second: 40,
      active: false
    },{
      id: 4,
      level: 4,
      second: 30,
      active: false
    },{
      id: 5,
      level: 5,
      second: 25,
      active: false
    },{
      id: 6,
      level: 6,
      second: 20,
      active: false
    },{
      id: 7,
      level: 7,
      second: 15,
      active: false
    },{
      id: 8,
      level: 8,
      second: 13,
      active: false
    },{
      id: 9,
      level: 9,
      second: 10,
      active: false
    }],
    testType: 1
  },
  onLoad( options ) {
    wxApi.setNavTitle('右脑训练')
    this.init( options );
  },
  init( options ) {
    this.seletedIndex = -1;
    this.testLevel = null;
  },
  onShow() {
  },
  onNavbar(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      testType: index
    })
  },
  onSelectLevel( e ) {
    let dataset = e.currentTarget.dataset
    let index = dataset.index;
    let newLevels = this.selectItem( index );
    this.testLevel = dataset.level;
    this.setData({
      levels: newLevels
    })
  },
  onStartTraining() {
    let params = {
      type: this.data.testType
    }
    let path = 'start-training/start-training';
    wxApi.navigateTo(path, params);
  },
  onViewCodes() {
    let path = 'code-list/code-list';
    let params = {
      type: this.data.testType
    }
    wxApi.navigateTo(path, params);
  },
  onStartTest() {
    if ( !this.testLevel || this.testLevel == 'null') {
      wx.showToast({
        title: '请选取测试级别',
        icon: 'none'
      })
      return
    }
    wxApi.navigateTo('start-test/start-test', {
      level: JSON.stringify( this.testLevel ),
      type: this.data.testType
    });
  },
  selectItem(index) {
    let levels = this.data.levels;
    let active = levels[ index ].active;
    for ( let i = 0, len = levels.length; i < len; i++ ) {
      levels[ i ].active = false;
    }
    levels[ index ].active = !active;
    return levels;
  }
})
